﻿Public Module ModuleTags
	Sub GenerateListViewTags(ByRef listview As ListView)
		listview.Items.Clear()
		listview.Columns.Clear()
		listview.Groups.Clear()
		listview.Activation = ItemActivation.TwoClick
		listview.View = View.LargeIcon


		Dim columnName As New Windows.Forms.ColumnHeader()
		Dim columnURL As New Windows.Forms.ColumnHeader()
		Dim columnReach As New Windows.Forms.ColumnHeader()
		Dim columnTaggings As New Windows.Forms.ColumnHeader()

		columnName.Name = "Name"
		columnName.Text = columnName.Name
		columnName.Tag = columnName.Name
		columnName.Width = 200

		columnURL.Name = "URL"
		columnURL.Text = columnURL.Name
		columnURL.Tag = columnURL.Name
		columnURL.Width = 120

		columnReach.Name = "Reach"
		columnReach.Text = columnReach.Name
		columnReach.Tag = columnReach.Name
		columnReach.Width = 120

		columnTaggings.Name = "Taggings"
		columnTaggings.Text = columnTaggings.Name
		columnTaggings.Tag = columnTaggings.Name
		columnTaggings.Width = 100

		Dim groupTags As New ListViewGroup("Tags", "Tags")

		groupTags.Tag() = "Tags"

		listview.Columns.AddRange({columnName, columnURL, columnReach, columnTaggings})
		listview.Groups.AddRange({groupTags})

		listview.SmallImageList = New ImageList()
		listview.SmallImageList.ImageSize = New Size(34, 34)
		listview.SmallImageList.Images.Add(My.Resources.image_unavailable00)
	End Sub

	Function GenerateListViewItemTags(name As String, url As String, reach As String, taggings As String, imageindex As Integer, ByRef group As ListViewGroup) As ListViewItem

		Dim subItemName As New ListViewItem.ListViewSubItem()
		Dim subItemURL As New ListViewItem.ListViewSubItem()
		Dim subItemReach As New ListViewItem.ListViewSubItem()
		Dim subItemTaggings As New ListViewItem.ListViewSubItem()

		subItemName.Tag = "Name"
		subItemName.Name = subItemName.Tag
		subItemName.Text = name

		subItemURL.Tag = "URL"
		subItemURL.Name = subItemURL.Tag
		subItemURL.Text = url

		subItemReach.Tag = "Reach"
		subItemReach.Name = subItemReach.Tag
		subItemReach.Text = reach

		subItemTaggings.Tag = "Taggings"
		subItemTaggings.Name = subItemTaggings.Tag
		subItemTaggings.Text = taggings

		Return New ListViewItem({subItemName, subItemURL, subItemReach, subItemTaggings}, imageindex, group)
	End Function

	Sub AddToListView(ByRef tags() As Tag, ByRef listview As ListView)
		If IsNothing(tags) Then
			Return
		End If

		Dim art As New PictureBox

		For Each tag As Tag In tags
			Dim item As ListViewItem = GenerateListViewItemTags(tag.name, tag.url, tag.reach, tag.taggings, 0, listview.Groups("Tags"))
			If Not (IsNothing(tag.image)) Then
				If tag.image(0) <> Nothing Then
					art.Load(tag.image(0))
					listview.SmallImageList.Images.Add(art.Image)
					item.ImageIndex = listview.SmallImageList.Images.Count - 1
				End If
			End If

			listview.Items.Add(item)
		Next


		listview.LargeImageList = listview.SmallImageList
		art.Dispose()
	End Sub
End Module
